package com.tina.controller;

import com.alibaba.fastjson.JSONObject;
import com.tina.entity.ImgVo;
import com.tina.entity.PageEntity;
import com.tina.util.FileUtils;
import com.tina.util.IDWorker;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

/**
 * @Author tina
 * @Time 2020/11/24 下午2:52
 * @Version 1.0
 * @Content 图片控制器
 **/
@Slf4j
@RestController
public class ImgController {
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * 查询图片
     *
     * @return
     * @throws IOException
     */
    @GetMapping("list")
    @CrossOrigin
    public PageEntity list() throws IOException {

        SearchRequest request = new SearchRequest("tina_img_photo");
        SearchSourceBuilder builder = new SearchSourceBuilder();

        // 分页
        builder.from(1);
        builder.size(200);

        request.source(builder);
        SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        long totalCount = search.getHits().getTotalHits().value;

        // 接收结果
        ArrayList<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();

        for (SearchHit hit : search.getHits().getHits()) {
            arrayList.add(hit.getSourceAsMap());
        }

        return new PageEntity(totalCount, arrayList, null, null);
    }

    /**
     * 将从文件地址中查询到的数据丢到ES中
     *
     * @return
     * @throws IOException
     */
    @GetMapping("img")
    public Boolean img() throws IOException {
        ArrayList<ImgVo> file = FileUtils.getFiles("/media/tina/UNTITLED/software/apache-tomcat-9.0.22/webapps/Photo/liuye/");
        for (ImgVo imgVo : file) {
            log.info("imgVo->{}", imgVo);
        }
        return bulk(file);
    }


    /**
     * 把数据丢进ES中
     *
     * @param file
     * @return
     * @throws IOException
     */
    public Boolean bulk(ArrayList<ImgVo> file) throws IOException {
        BulkRequest bulkRequest = new BulkRequest();


        IDWorker idWorker = new IDWorker(1, 2);
        for (int i = 0; i < file.size(); i++) {
            bulkRequest.add(
                    new IndexRequest("tina_img_photo")
                            .id(String.valueOf(idWorker.nextId())).source(JSONObject.toJSONString(file.get(i)), XContentType.JSON));
        }
        // 成功 false 失败true
        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        return bulk.hasFailures();
    }


}
